package com.yaokai.springbootreabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueryConfig {

    //延时交换机名称
    public static final String DELAYED_EXCHANGE = "delayed.exchange";
    //绑定
    public static final String DELAYED_ROUTINGKEY = "delayed.routingkey";
    //队列名称
    public static final String DELAYED_QUERY = "delayed.query";

    // 声明 delayed_query 队列
    @Bean("delayed_query")
    public Queue delayedQUERY() {
        return new Queue(DELAYED_QUERY);
    }

    // 声明 delayed_exchange 交换机
    @Bean("delayed_exchange")
    public CustomExchange delayedEXCHANGE() {
        Map<String, Object> args = new HashMap<>();
        //自定义交换机的类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    //声明队列 B 绑定 X 交换机
    @Bean
    public Binding delayedROUTINGKEY(@Qualifier("delayed_query") Queue delayed_query,
                                  @Qualifier("delayed_exchange") CustomExchange delayed_exchange) {
        return BindingBuilder.bind(delayed_query).to(delayed_exchange).with(DELAYED_ROUTINGKEY).noargs();
    }


}
